package com.ruoyi.animal.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.animal.domain.AnimalInformationHistory;
import com.ruoyi.animal.domain.AnimalSpecies;
import com.ruoyi.animal.service.ExperimentDesignService;
import com.ruoyi.animal.service.IAnimalInformationHistoryService;
import com.ruoyi.animal.service.IAnimalSpeciesService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.file.controller.SysFileController;
import com.ruoyi.file.utils.FileUploadUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.animal.domain.AnimalInformation;
import com.ruoyi.animal.service.IAnimalInformationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 动物信息Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/information")
public class AnimalInformationController extends BaseController
{
    @Autowired
    private IAnimalInformationService animalInformationService;

    @Autowired
    private IAnimalSpeciesService animalSpeciesService;

    @Autowired
    private ExperimentDesignService experimentDesignService;

    @Autowired
    private RemoteFileService remoteFileService;


    /**
     * 查询动物信息列表
     */
    @RequiresPermissions("animal:information:list")
    @GetMapping("/list")
    public TableDataInfo list(AnimalInformation animalInformation)
    {
        startPage();
        List<AnimalInformation> list = animalInformationService.selectAnimalInformationList(animalInformation);
        for(AnimalInformation animalInformation1:list){
            AnimalSpecies animalSpecies = animalSpeciesService.selectAnimalSpeciesBySpeciesId(animalInformation1.getSpeciesId());
            AnimalSpecies animalSpecies1 = animalSpeciesService.selectAnimalSpeciesBySpeciesId(animalInformation1.getBreed());


            if(animalSpecies!=null && animalSpecies1!=null) {
                animalInformation1.setSpeciesName(animalSpecies.getSpeciesName());
                animalInformation1.setBreedName(animalSpecies1.getSpeciesName());
            }
            if(animalInformation1.getDesignId()!=null) {
                AjaxResult ajaxResult = experimentDesignService.getInfo(animalInformation1.getDesignId());
                if(ajaxResult!=null) {
                    Map map = (Map) ajaxResult.get("data");
                    animalInformation1.setDesignName(map.get("designName").toString());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询动物信息列表
     */
    @PostMapping("/list1")
    public List<AnimalInformation> list1(AnimalInformation animalInformation)
    {
        List<AnimalInformation> list = animalInformationService.selectAnimalInformationList(animalInformation);
        for(AnimalInformation animalInformation1:list){
            AnimalSpecies animalSpecies = animalSpeciesService.selectAnimalSpeciesBySpeciesId(animalInformation1.getSpeciesId());
            AnimalSpecies animalSpecies1 = animalSpeciesService.selectAnimalSpeciesBySpeciesId(animalInformation1.getBreed());


            if(animalSpecies!=null && animalSpecies1!=null) {
                animalInformation1.setSpeciesName(animalSpecies.getSpeciesName());
                animalInformation1.setBreedName(animalSpecies1.getSpeciesName());
            }
            System.out.println(animalInformation1.toString());
        }
        return list;
    }

    /**
     * 导出动物信息列表
     */
    @RequiresPermissions("animal:information:export")
    @Log(title = "动物信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AnimalInformation animalInformation)
    {
        List<AnimalInformation> list = animalInformationService.selectAnimalInformationList(animalInformation);
        ExcelUtil<AnimalInformation> util = new ExcelUtil<AnimalInformation>(AnimalInformation.class);
        util.exportExcel(response, list, "动物信息数据");
    }

    /**
     * 获取动物信息详细信息
     */
    @RequiresPermissions("animal:information:query")
    @GetMapping(value = "/{animalId}")
    public AjaxResult getInfo(@PathVariable("animalId") Long animalId)
    {
        return success(animalInformationService.selectAnimalInformationByAnimalId(animalId));
    }

    /**
     * 新增动物信息
     */
    @RequiresPermissions("animal:information:add")
    @Log(title = "动物信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AnimalInformation animalInformation)
    {
        return toAjax(animalInformationService.insertAnimalInformation(animalInformation));
    }

    /**
     * 修改动物信息
     */
    @RequiresPermissions("animal:information:edit")
    @Log(title = "动物信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AnimalInformation animalInformation)
    {
        return toAjax(animalInformationService.updateAnimalInformation(animalInformation));
    }

    /**
     * 删除动物信息
     */
    @RequiresPermissions("animal:information:remove")
    @Log(title = "动物信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{animalIds}")
    public AjaxResult remove(@PathVariable Long[] animalIds)
    {
        return toAjax(animalInformationService.deleteAnimalInformationByAnimalIds(animalIds));
    }

    //回溯数据
    @PutMapping("/restoreData")
    public AjaxResult restoreData(@RequestBody AnimalInformationHistory animalInformationHistory)
    {
        AnimalInformation animalInformation = new AnimalInformation();
        BeanUtils.copyProperties(animalInformationHistory,animalInformation);
        return toAjax(animalInformationService.updateAnimalInformation(animalInformation));
    }

    // 图片校验
    @PostMapping("/checkImage")
    public AjaxResult checkImage(@RequestBody AnimalInformation animalInformation) {
            String animalImage = animalInformation.getAnimalImage();
            // 获取文件
            String fileMd5 = remoteFileService.getFileMd5(animalImage);
            if(fileMd5 == null){
                return AjaxResult.error("图片校验失败");
            }
            if(!animalInformation.getMd5Hash().equals(fileMd5)){
                return AjaxResult.error("图片被修改");
            }
            return AjaxResult.success("图片校验成功");
    }

}
